#include "ParallaxBackground.h"
#include "GameConfiguration.h"
#include "WindowManager.h"
#include "SpritesHolder.h"
#include "Player.h"

ParallaxBackground* ParallaxBackground::instance = NULL;

ParallaxBackground::ParallaxBackground(int x, int y) : Sprite(x, y)
{
	surface = SDL_CreateTexture(WindowManager::GetSRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, gameconfig::SCREEN_WIDTH, gameconfig::SCREEN_HEIGHT);
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = 1280;
	srcRect.h = 720;

	layer1 = SDL_CreateTexture(WindowManager::GetSRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, gameconfig::SCREEN_WIDTH, gameconfig::SCREEN_HEIGHT * 2);
	l1SrcRect.x = 0;
	l1SrcRect.y = 720;
	l1SrcRect.w = 1280;
	l1SrcRect.h = 720;

	sky = Assets::background;
	skySrcRect.x = 0;
	skySrcRect.y = 0;
	skySrcRect.w = 1280;
	skySrcRect.h = 1440;

	this->box2DBody = Box2D::CreateStaticBody(0, 710, srcRect.w / 2, srcRect.h / 2);
	instance = this;
}

ParallaxBackground::~ParallaxBackground(void)
{
}

void ParallaxBackground::Update()
{
	//logger->info("Background X =", box2DBody->GetPosition().x);
	//logger->info("Background Y =", box2DBody->GetPosition().y);
}


void ParallaxBackground::Move(int x, int y)
{
	Player *player = Player::getInstance();
	l1SrcRect.y -= y;

	if (Player::getInstance()->dstRect.x >= 1000)
	{
		SpritesHolder::GetInstance()->MoveSprites(x);
	}

	if (Player::getInstance()->dstRect.x <= 200 && Player::getInstance()->trueXPos >= 1280)
	{
		SpritesHolder::GetInstance()->MoveSprites(x);
	}
}

ParallaxBackground* ParallaxBackground::GetInstance()
{
	return instance;
}